package com.example.xlmkotlinwanandroid.ui.fragment


import android.content.Context
import android.os.Bundle
import android.view.*
import androidx.core.view.GravityCompat
import androidx.databinding.DataBindingUtil
import androidx.fragment.app.viewModels
import androidx.paging.PagedList
import androidx.recyclerview.widget.LinearLayoutManager
import com.example.xlmkotlinwanandroid.R
import com.example.xlmkotlinwanandroid.base.BaseFragment
import com.example.xlmkotlinwanandroid.base.BaseVMFragment
import com.example.xlmkotlinwanandroid.data.Resource
import com.example.xlmkotlinwanandroid.databinding.FragmentHomeBinding
import com.example.xlmkotlinwanandroid.ext.dp2Px
import com.example.xlmkotlinwanandroid.ext.observer
import com.example.xlmkotlinwanandroid.ext.toGone
import com.example.xlmkotlinwanandroid.ext.toVisible
import com.example.xlmkotlinwanandroid.model.bean.BannerBean
import com.example.xlmkotlinwanandroid.model.viewmodel.HomeViewModel
import com.example.xlmkotlinwanandroid.network.ArticlePagedListAdapter2
import com.example.xlmkotlinwanandroid.network.ArticleResponse2
import com.example.xlmkotlinwanandroid.network.ArticleViewModel2
import com.example.xlmkotlinwanandroid.ui.activity.MainActivity
import com.example.xlmkotlinwanandroid.ui.adapter.ArticleAdapter
import com.example.xlmkotlinwanandroid.ui.adapter.MyBannerAdapter
import com.example.xlmkotlinwanandroid.view.SpaceItemDecoration
import com.youth.banner.Banner
import com.youth.banner.indicator.CircleIndicator


/**
 * 首页的fragment
 */
class HomeFragment : BaseVMFragment<HomeViewModel>() {
    //homeViewModel
//    private val homeViewModel by viewModels<HomeViewModel>()
    private var bannerList = ArrayList<BannerBean>()

    //懒加载adapter
    private val bannerAdapter by lazy {
        MyBannerAdapter(activity as Context, bannerList)
    }
    private val articleAdapter by lazy { ArticleAdapter(activity) }

    private val articleAdapter2 by lazy { ArticlePagedListAdapter2(context) }

    private lateinit var homeBinding: FragmentHomeBinding

    //初始化数据
    override fun initData() {
        viewModel.getMyBannerLiveData()
    }

    private fun updateArticle(it: PagedList<ArticleResponse2.ArticleBean2>?) {
        articleAdapter2.submitList(it)
    }

    private fun updateBannerView(status: Resource<ArrayList<BannerBean>>) {
        when (status) {
            is Resource.Loading -> homeBinding.loaderView.toVisible()
            is Resource.DataError -> {
                homeBinding.loaderView.toGone()
                viewModel.showToastMessage("error ...")
            }
            is Resource.Success -> status.data?.let {
                homeBinding.loaderView.toGone()
                bannerAdapter.setData(it)
                bannerAdapter.notifyDataSetChanged()
            }
        }
    }

    override fun initViews() {
        //设置activity的toolbar
        setUpToolbar()
        //加载banner对象
        setUpBannerView()
    }

    private fun setUpBannerView() {
        val bannerView = layoutInflater.inflate(R.layout.item_banner, null, false)
        val banner = bannerView.findViewById<Banner<BannerBean, MyBannerAdapter>>(R.id.banner)

        banner.setAdapter(bannerAdapter)
            .addBannerLifecycleObserver(this).indicator = CircleIndicator(activity)
        //讲banner加入到头文件
        homeBinding.recyclerView.addHeaderView(bannerView)
        //recyclerView初始化
        homeBinding.recyclerView.layoutManager =
            LinearLayoutManager(requireActivity(), LinearLayoutManager.VERTICAL, false)
        homeBinding.recyclerView.addItemDecoration(SpaceItemDecoration(0, 8f.dp2Px(), false))
        homeBinding.recyclerView.adapter = articleAdapter2
        // TODO: 2022/2/10 0010 下拉刷新和上拉加载更多
//        homeBinding.swipeRefresh.setOnRefreshListener {
//            viewModel.getHomeLiveData(true)
//        }
    }

    private fun setUpToolbar() {
        val myActivity = (activity as MainActivity)
        homeBinding.toolbar.title = "首页"
        myActivity.setSupportActionBar(homeBinding.toolbar)
        myActivity.supportActionBar?.let {
            it.setDisplayHomeAsUpEnabled(true)
            it.setHomeAsUpIndicator(R.drawable.nav)
        }
        setHasOptionsMenu(true)
    }

    override fun viewBinding(inflater: LayoutInflater, container: ViewGroup?): View {
        homeBinding = DataBindingUtil.inflate(inflater, R.layout.fragment_home, container, false);
        return homeBinding.root
    }

    override fun viewModelClass(): Class<HomeViewModel> {
        return HomeViewModel::class.java
    }

    override fun observeViewModel() {
        observer(viewModel.bannerObservableData, ::updateBannerView)
        observer(ArticleViewModel2().articlePageList, ::updateArticle)
    }

    override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater) {
        super.onCreateOptionsMenu(menu, inflater)
    }

    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        when (item.itemId) {
            android.R.id.home -> homeBinding.drawLayout.openDrawer(GravityCompat.START)
        }
        return true
    }

}